
/**
 * @description 扁平结构转换树形结构
 * @param {Array} data 扁平数组对象
 * @returns 树形数组对象
 */
function tranTree(data) {
  const obj = {};
  data.forEach(item => {
    obj[item.id] = item; 
  });
  // * obj -> {1001: {id: 1001, parentId: 0, name: 'AA'}, 1002: {...}}
  const treeData = []; 
  data.forEach((item) => {
    const parent = obj[item.pid]; 
    if (parent) {
      // * 当前项有父节点
      parent.children = parent.children || [];
      parent.children.push(item);
    } else {
      // * 当前项没有父节点 -> 顶层
      treeData.push(item);
    }
    
  });
  return treeData;
}
  export default tranTree
 